package servlet;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Answer;
import model.ExpiredQuest;
import model.Participation;
import model.Quest;
import DAO.AnswerDao;
import DAO.ExpiredQuestDao;
import DAO.ParticipationDao;
import DAO.QuestDao;
import DAO.UserDetailDao;

public class QuestListAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ActionForward forward = new ActionForward();
		ArrayList<Quest> questIdList = new ArrayList<Quest>();
		ArrayList<Quest> questList = new ArrayList<Quest>();
		ArrayList<Answer> answerList = new ArrayList<Answer>();
		QuestDao questDao = new QuestDao();
		request.setAttribute("checkValue", "checkValue");
		HttpSession session = request.getSession();
		AnswerDao answerDao = new AnswerDao();

		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userIdCheck = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String) session.getAttribute("userid")) : 0;

		if (userIdCheck < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}

		questIdList = questDao.getAllQuestList(studyId);

		Date now = new Date();
		long timeTemp = now.getTime() - 86400000;

		Timestamp nowTimeStamp = new Timestamp((timeTemp));
		for (int i = 0; i < questIdList.size(); i++) {

			Quest quest = questDao
					.getQuestInfo(questIdList.get(i).getQuestId());

			ExpiredQuestDao expiredQuestDao = new ExpiredQuestDao();
			if (expiredQuestDao.getAllExpiredQuest(quest.getQuestId())) {
				quest.setExpired(true);
			} else {

				Timestamp expiredTimeStamp = quest.getExpiredTime();

				if (nowTimeStamp.after(expiredTimeStamp)) {

					ExpiredQuest expiredQuest = new ExpiredQuest();
					expiredQuest.setSid(studyId);
					expiredQuest.setQid(quest.getQuestId());
					expiredQuest.setExpiredTime(expiredTimeStamp);
					expiredQuestDao.insertExpiredQuest(expiredQuest);

					ArrayList<Answer> answerListForUserDetail = answerDao
							.getAllAnswerList(expiredQuest.getQid());

					UserDetailDao userDetailDao = new UserDetailDao();

					for (int j = 0; j < answerListForUserDetail.size(); j++) {
					
						userDetailDao.updateClearQuest(answerListForUserDetail
								.get(j).getUid());

					}

					quest.setExpired(true);
				}
			}

			questList.add(quest);

		}
		Answer answer = new Answer();
	

		ArrayList<Answer> answerListTemp = answerDao.getAllAnswerList();


		for (int j = 0; j < answerListTemp.size(); j++) {
		
			if (!answerListTemp.get(j).getContent().equals("답변을 작성해주세요.")) {
				answerList.add(answerListTemp.get(j));

			}

		}

		request.setAttribute("studyId", studyId);
		request.setAttribute("questList", questList);
		request.setAttribute("answerList", answerList);

		forward.setPath("./study_quest.jsp");

		return forward;
	}

}
